//
//  WBTabBar.swift
//  WeiBo
//
//  Created by liushaohua on 16/10/22.
//  Copyright © 2016年 liushaohua. All rights reserved.
//

import UIKit

class WBTabBar: UITabBar {
    
    // 定义一个闭包
    var closure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    
    }
    
    // xib 或者Storyboard 创建是调用的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:搭建界面
    func setupUI(){
     
     addSubview(composeButton)
    }
    
    //MARK: 创建按钮的点击方法
    func composeBtnClick(){
        closure?()
    }
    //MARK: 布局子空间
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = LLSCREENW * 0.2
        
        var  index:CGFloat =  0
        
        
        for item in subviews {
   
            // 只遍历系统提供的按钮
            if item.isKind(of: NSClassFromString("UITabBarButton")!) {
   
                if index == 2 {
                    index += 1
                }
                
                item.bounds.size.width = w
                item.frame.origin.x = w * index
                index += 1
            }

        }
        composeButton.center.x = LLSCREENW * 0.5
        composeButton.center.y = frame.size.height * 0.5
        
    }
 
    //MARK: 懒加载创建中间按钮
   lazy var composeButton: UIButton = {
    
    let Btn = UIButton(setImage: "tabbar_compose_icon_add", setBackgroundImage: "tabbar_compose_button", target: self, action: #selector(composeBtnClick))
    
//    Btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: UIControlState.normal)
//    Btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
//    Btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: UIControlState.normal)
//    Btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
//    Btn.addTarget(self, action: #selector(composeBtnClick), for: UIControlEvents.touchUpInside)
    
  
    
     return Btn
    }()
    
    
  
    
    
    
    
    
    
}
